using LiteDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    public class Customer
    {
        public int CustomerId { get; set; }
        public string Name { get; set; }
    }

    public class Order
    {
        public int OrderId { get; set; }
        [BsonRef]
        public Customer Customer { get; set; }
    }

    public class Order2
    {
        public Customer customer { get; set; }

        public string name { get; set; }
    }

    public static class TestLiteDb
    {
        public static void TestId()
        {
            var order = new Order2
            {
                name = "dsfds",
                customer = new Customer { CustomerId = 99, Name = "John Doe" }
            };

            using (var db = new LiteDB.LiteDatabase("test.db"))
            {
                var col = db.GetCollection<Order2>();
                col.Insert(order);
            }

            using (var db = new LiteDB.LiteDatabase("test.db"))
            {
                var col = db.GetCollection<Order2>();
                var items = col.FindAll();
                foreach (var item in items)
                {
                    Console.WriteLine("id:{0}, customerid:{1}, customername:{2}", item.name, item.customer.CustomerId, item.customer.Name);
                }
            }
        }

        public static void TestDbRef()
        {
            var order = new Order
            {
                OrderId = 123,
                Customer = new Customer { CustomerId = 99, Name = "John Doe" }
            };
            using (var db = new LiteDB.LiteDatabase("test.db"))
            {
                var col = db.GetCollection<Order>();
                col.Insert(order);
                var col2 = db.GetCollection<Customer>();
                col2.Insert(order.Customer);
            }
            using (var db = new LiteDB.LiteDatabase("test.db"))
            {
                var col = db.GetCollection<Order>();
                col = col.Include(item => item.Customer);
                var items = col.FindAll();
                foreach (var item in items)
                {
                    Console.WriteLine("id:{0}, customerid:{1}, customername:{2}", item.OrderId, item.Customer.CustomerId, item.Customer.Name);
                }
            }
        }
    }
}
